package com.ykq.order.controller;

import com.ykq.Order;
import com.ykq.Product;
import com.ykq.order.feign.ProductFeign;
import com.ykq.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private ProductFeign productFeign;


//    @Autowired
//    private RestTemplate restTemplate;


    @GetMapping("createOrder/{pid}")
    //@HystrixCommand(fallbackMethod = "fallbackMethod")
    //容错的注解  fallbackMethod: 当服务出错时，执行fallbackMethod指定的方法（备胎）
    //fallback指定的方法必须与该方法参数相同，返回类型相同。
    public String createOrder(@PathVariable("pid")Integer pid){
        Order order=new Order();
        order.setUid(1);
        order.setUsername("张三");
        order.setNumber(1);
        Product product = productFeign.findById(pid);
        if(product.getPid()==-1){
            return "服务正忙";
        }
        order.setPprice(product.getPprice());
        order.setPname(product.getPname());
        order.setPid(product.getPid());
        orderService.createOrder(order);
        return "下单成功";
    }



    @GetMapping("getOrder")
    public String getOrder(){
        return "该订单的信息如下:华为手机！";
    }
}
